// 引入接口文件
import { exportList,queryestateList,getAreaList,queryAllAddress,getReason} from './commissionListServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download, makeAddress } from 'utils/util'


// 初始默认状态
const defultState = {
  exportLoading: false,
  loading: false,
  btnLoading: false,
  areaList: [],
  cityList: [],
  estateList:[],
  commissionInfo:{},
  defaultExpandedRowKey:[],
  showTabelModal:false,
  actionType:'reasonSearch',
  modalTitle:'',
  detailData:{
      city:[],
      productId:'',
      mobile:"",
      reason:""
  },
  biz:{
    queryForm:{
      startTime:moment().startOf('month').format('YYYY-MM-DD'),
      endTime:moment().endOf('month').format('YYYY-MM-DD'),
    }
  }
}

// 声明module
const tmpModule = {
  // namespace: 'productListModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          // dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState), ...query} });//每次进去页面先重置状态
          // 执行业务操作
          dispatch({ type: 'getAreaList', payload: {} })
          dispatch({ type: 'getCityList', payload: {} })
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    
    // 获取区域地址
    *getAreaList({ payload }, { put, call, select }) {
      let {modalObj,} = yield select(state => state[tmpModule.namespace])
      try {
      
        const { success, data=[] } = yield call(getAreaList,{})
        if (success) {
          let areaList = []
          data.map((item,index)=>{
              areaList.push({
                value: item.regionId,
                label: item.regionName
              })
          })
          yield put({ type: 'updateStore', payload: { areaList } });
        }
      } catch (e) {
      }
    },
    
    //导出
    * exportList ({payload}, {put, call, select}) {
      let {exportLoading,biz:{queryForm}} = yield select(state => state[tmpModule.namespace])
      //组装接口入参
      let params = {
        // orgId, agentConfigId
        ...queryForm,
      }
      try{
        yield put({type: 'updateStore', payload: {exportLoading:true}})
        let {data = {}, success, msg} = yield call(exportList, {...params})
        yield put({type: 'updateStore', payload: {exportLoading:false}})
        if (!success) {
          message.error(msg)
          return false
        }
        if (data&&data.url) {
          download(data.url)
        }
      }catch (err){
        yield put({type: 'updateStore', payload: {exportLoading:false}})
      }
    },
    *getCityList({ payload }, { put, call, select }) {
        try {
            const data = yield call(queryAllAddress, {})
            if (data.success) {
                let cityList = []
                let arr = [data.data[0]]
                let attr = [data.data[data.data.length-1]]
                data.data[0] = {
                    label:"北京",
                    value:"110100",
                    children:arr
                }
                data.data[data.data.length-1]={
                    label:"澳门",
                    value:"820000",
                    children:attr
                }
                //去掉第三级，
                cityList = makeAddress(data.data,2,false)
                yield put({ type: 'updateStore', payload: { cityList: cityList } });
            }
        } catch (e) {

        }
    },
    *getEstateList({ payload }, { put, call, select }) {
        try {
            let tmpState = yield select(state => state[tmpModule.namespace])
            // let { buildInfo } = tmpState
            const { resolve,typeGet } = payload;
            let params = {
                pageNo: 1,
                pageSize: 1000,
                ...payload
        }
            const data = yield call(queryestateList, params)
            let estateList = []
            let zone
            if (data.success) {
                estateList = data.rows
                !!resolve && resolve(data.rows);
            }

            yield put({ type: 'updateStore', payload: { estateList} });

        } catch (e) {
        }

    },
    *getReason({ payload }, { put, call, select }) {
        try {
            const data = yield call(getReason, payload)
            let commissionInfo={}
            if (data.success) {
                commissionInfo.reason = data.data.reason
            }
            yield put({ type: 'updateStore', payload: {commissionInfo}});

        } catch (e) {
        }
    },
    *reasonSearch({ payload }, { put,select}) {
        yield put({type: 'updateStore', payload });
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
